const router = require("koa-router")();
const path = require("path");
const fs = require("fs");
const city = require("../mock/cities.json");
const list = require("../mock/list.json");
const tripTrainData = require("../mock/tripTrain.json");

router.get("/trip/airplane", async (ctx, next) => {
  const { start, end, page = 1 } = ctx.query;
  const url = `${start}-${end}.json`;
  const url1 = path.resolve(__dirname, "../mock", url);
  const result = fs.existsSync(url1);
  if (result) {
    const data = require(url1);
    const sliceData = data.slice((page - 1) * 10, page * 10);
    if (sliceData.length) {
      ctx.body = { code: 200, message: "数据获取成功", data: sliceData };
    } else {
      ctx.body = { code: 200, message: "没有更多数据了", data: sliceData };
    }
  } else {
    ctx.body = { code: 404, message: "该飞行暂没有数据支持" };
  }
});

router.post("/trip/train", async (ctx, next) => {
  const {
    start,
    end,
    page = 1,
    isSpeed,
    isSeat,
    isDesc = true,
  } = ctx.request.body;

  const newData = [...tripTrainData]
    .filter((v) => {
      return v.start === start && v.end === end;
    })
    .filter((v) => {
      if (isSpeed) {
        return v.isSpeed;
      } else {
        return true;
      }
    })
    .filter((v) => {
      if (isSeat) {
        return v.isSeat;
      } else {
        return true;
      }
    })
    .sort((a, b) => {
      console.log();
      if (isDesc)
        return (
          new Date(
            new Date().toLocaleDateString() + " " + a.startTime
          ).getTime() -
          new Date(
            new Date().toLocaleDateString() + " " + b.startTime
          ).getTime()
        );
      return (
        new Date(
          new Date().toLocaleDateString() + " " + b.startTime
        ).getTime() -
        new Date(new Date().toLocaleDateString() + " " + a.startTime).getTime()
      );
    });

  const data = newData.slice((page - 1) * 10, page * 10);
  ctx.body = {
    code: 200,
    message: "数据获取成功",
    total: newData.length,
    data,
  };
});

router.get("/trip/train/detail", async (ctx) => {
  const { id } = ctx.request.query;
  const item = tripTrainData.find((v) => v.id === id);
  ctx.body = {
    code: 200,
    message: "数据获取成功",
    data: item,
  };
});

router.get("/city", async (ctx, next) => {
  ctx.body = {
    code: 200,
    data: city,
    message: "ok",
  };
});

router.get("/city/search", async (ctx, next) => {
  const { wd = "" } = ctx.request.query;
  const result = [];
  city.city.forEach((v) => {
    v.list.forEach((v) => {
      v.name.includes(wd) && result.push(v);
    });
  });

  ctx.body = {
    code: 200,
    message: "ok",
    data: result,
  };
});

router.get("/list", async (ctx, next) => {
  const { page = 1, pageSize = 10, wd = "" } = ctx.request.query;
  let newData = [...list];
  if (wd) {
    newData = newData.filter((item) => {
      return JSON.stringify(item).includes(wd);
    });
  }
  newData = newData.slice((page - 1) * pageSize, page * pageSize);

  ctx.body = {
    code: 200,
    data: newData,
    message: "ok",
  };
});

router.get("/list/detail", async (ctx, next) => {
  const { id } = ctx.request.query;
  const data = list.find((item) => item.id === id);
  ctx.body = {
    code: 200,
    data,
    message: "ok",
  };
});

const Mock = require("mockjs");
const data = Mock.mock({
  "list|100": [
    {
      id: "@id",
      image: "@image(150x150, @color)",
      title: "@ctitle",
      desc: "@cword(10, 30)",
      "price|10-1000": 1,
    },
  ],
});
router.get("/long/list", (ctx) => {
  ctx.body = {
    code: 200,
    message: "ok",
    data: data.list,
  };
});

router.get("/long/list/detail", (ctx) => {
  const { id } = ctx.request.query;
  const newData = data.list.find((item) => item.id === id);
  if (newData) {
    ctx.body = {
      code: 200,
      newData,
      message: "ok",
    };
  } else {
    ctx.body = {
      code: 404,
      message: "未找到该数据",
    };
  }
});

module.exports = router;
